<?php
namespace Admin\Controller;
use Think\Controller;
class ReserveController extends CommonController {
	//订单显示
    public function index()
    {
        $reserve = M('reserve'); // 实例化User对象
        $count      = $reserve->where('is_del=0')->count();// 查询满足要求的总记录数
        $Page       = new \Think\Page($count,20);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show       = $Page->show();// 分页显示输出

        //查询keyword动作
        $keyword = I('get.keyword');
        if (!empty($keyword)) {
            if (is_numeric($keyword)) {
                $sql = "select * from reserve where is_del=0 and goods_id='{$keyword}' limit ". $Page->firstRow.','.$Page->listRows;
            }else {
                $sql = "select * from reserve where is_del=0 and name='{$keyword}' limit ". $Page->firstRow.','.$Page->listRows;
            }
        }else {
            $sql = 'select * from reserve where is_del=0 limit '. $Page->firstRow.','.$Page->listRows;
        }
        
        $reserve = M()->query($sql);
        $this->assign('reserve',$reserve);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出

        $this->display();
    }

    //处理状态 设为处理1
    public function is_chuli()
    {
        $id = I('post.id');
        $res = M('reserve')->where(array('id'=>$id))->setField('is_chuli',1);
        if ($res) {
            $arr = array(
                'error' => 1 ,
                'info' => '订单处理成功'
                );
            echo json_encode($arr);
        }else {
            $arr = array(
                'error' => 0 ,
                'info' => '订单处理失败'
                );
            echo json_encode($arr);
        }
    }

    //回收 设为1
    public function del()
    {
        $id = I('post.id');
        $res = M('reserve')->where(array('id'=>$id))->setField('is_del',1);
        if ($res) {
            $arr = array(
                'error' => 1 ,
                'info' => '订单回收成功'
                );
            echo json_encode($arr);
        }else {
            $arr = array(
                'error' => 0 ,
                'info' => '订单回收失败'
                );
            echo json_encode($arr);
        }
    }

    //批量回收
    public function dels()
    {
      if (IS_POST) {
        $arr = I('post.arr');
        $ids = implode(",", $arr);
        $w['id']=array('in',"$ids");
        
        $res = M('reserve')->where($w)->setField('is_del',1);
          if ($res) {
              $arr = array(
                  'error' =>1,
                  'info'=>"订单回收成功"
                  );
              echo json_encode($arr);
          }else {
              $arr = array(
                  'error' =>0,
                  'info'=>"订单回收失败"
                  );
              echo json_encode($arr);
          }
      }
    }

    //回收列表
    public function reservedel()
    {
        $reserve = M('reserve'); // 实例化User对象
        $count      = $reserve->where('is_del=1')->count();// 查询满足要求的总记录数
        $Page       = new \Think\Page($count,20);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show       = $Page->show();// 分页显示输出

        //查询keyword动作
        $keyword = I('get.keyword');
        if (!empty($keyword)) {
            if (is_numeric($keyword)) {
                $sql = "select * from reserve where is_del=1 and goods_id='{$keyword}' limit ". $Page->firstRow.','.$Page->listRows;
            }else {
                $sql = "select * from reserve where is_del=1 and name='{$keyword}' limit ". $Page->firstRow.','.$Page->listRows;
            }
        }else {
            $sql = 'select * from reserve where is_del=1 limit '. $Page->firstRow.','.$Page->listRows;
        }
        $reserve = M()->query($sql);
        $this->assign('reserve',$reserve);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出

        $this->display();
    }

    //恢复
    public function huifu()
    {
        $id = I('post.id');
        $res = M('reserve')->where(array('id'=>$id))->setField('is_del',0);
        if ($res) {
            $arr = array(
                'error' => 1 ,
                'info' => '订单恢复成功'
                );
            echo json_encode($arr);
        }else {
            $arr = array(
                'error' => 0 ,
                'info' => '订单恢复失败'
                );
            echo json_encode($arr);
        }
    }

    //删除
    public function delete()
    {
        $id = I('post.id');
        $res = M('reserve')->delete($id);
        if ($res) {
            $arr = array(
                'error' => 1 ,
                'info' => '订单删除成功'
                );
            echo json_encode($arr);
        }else {
            $arr = array(
                'error' => 0 ,
                'info' => '订单删除失败'
                );
            echo json_encode($arr);
        }
    }


}